package com.rede.didiok.user.service.impl;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.SQLConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.dto.user.TodoDto;
import com.rede.didiok.common.enums.StatusEnum;
import com.rede.didiok.common.enums.SwitchEnum;
import com.rede.didiok.common.holder.RequestHolder;
import com.rede.didiok.common.utils.PageUtils;
import com.rede.didiok.common.utils.Query;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.user.dao.TodoDao;
import com.rede.didiok.user.entity.TodoEntity;
import com.rede.didiok.user.service.TodoService;


@Service("todoService")
public class TodoServiceImpl extends ServiceImpl<TodoDao, TodoEntity> implements TodoService {

    @Autowired
    TodoService todoService;

    @Resource
    TodoDao todoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TodoEntity> page = this.page(
                new Query<TodoEntity>().getPage(params),
                new QueryWrapper<TodoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void toggleAll(Integer done, String adminUid) {
        todoDao.toggleAll(done, adminUid);
    }

    @Override
    public IPage<TodoEntity> getPageList(TodoDto todoVO) {
        String adminUid = RequestHolder.getAdminUid();
        QueryWrapper<TodoEntity> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(todoVO.getKeyword()) && !StringUtils.isEmpty(todoVO.getKeyword().trim())) {
            queryWrapper.like(SQLConf.TEXT, todoVO.getKeyword().trim());
        }
        queryWrapper.eq(SQLConf.ADMINUID, adminUid);

        //按时间顺序倒排
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);

        Page<TodoEntity> page = new Page<>();
        page.setCurrent(todoVO.getCurrentPage());
        page.setSize(todoVO.getPageSize());
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        IPage<TodoEntity> pageList = todoService.page(page, queryWrapper);
        return pageList;
    }

    @Override
    public String addTodo(TodoDto todoVO) {
        String adminUid = RequestHolder.getAdminUid();
        TodoEntity todo = new TodoEntity();
        todo.setText(todoVO.getText());
        //默认未完成
        todo.setDone(SwitchEnum.CLOSE.getCode());
        todo.setAdminUid(adminUid);
        todo.insert();
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String editTodo(TodoDto todoVO) {
        String adminUid = RequestHolder.getAdminUid();
        TodoEntity todo = todoService.getById(todoVO.getUid());

        if (!todo.getAdminUid().equals(adminUid)) {
            return ResultUtil.errorWithMessage(MessageConf.ACCESS_NO_PRIVILEGE);
        }

        todo.setText(todoVO.getText());
        todo.setDone(todoVO.getDone());
        todo.setUpdateTime(new Date());
        todo.updateById();
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String deleteTodo(TodoDto todoVO) {
        String adminUid = RequestHolder.getAdminUid();
        TodoEntity todo = todoService.getById(todoVO.getUid());

        if (!todo.getAdminUid().equals(adminUid)) {
            return ResultUtil.errorWithMessage(MessageConf.DATA_NO_PRIVILEGE);
        }

        todo.setStatus(StatusEnum.DISABLED);
        todo.setUpdateTime(new Date());
        todo.updateById();
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.DELETE_SUCCESS);
    }

    @Override
    public String editBatchTodo(TodoDto todoVO) {
        String adminUid = RequestHolder.getAdminUid();
        if (SwitchEnum.OPEN.getCode().equals(todoVO.getDone())) {
            todoService.toggleAll(SysConf.ONE, adminUid);
        } else {
            todoService.toggleAll(SysConf.ZERO, adminUid);
        }
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.UPDATE_SUCCESS);
    }

}